/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package csBakery;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

/*
 *	@release Final Release
 *	@author  Lee Cheuk Wang
 *	@version 1.1.5
 */


public class BalanceController {
	private PriceList pricelist;
	private CostList costlist;
	private LogController logcontroller;
	private Vector<LogController.LogItem> logData;
	
	public BalanceController(String pricefile, String costfile, String logfile){
		pricelist = new PriceList(pricefile);
		costlist = new CostList(costfile);
		logcontroller = new LogController(logfile);
		logData = logcontroller.getLog();
	}
	
	//Generate today balance
	public String generateDayBalance(){
		
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String today = df.format(thisDate);

		return balanceSheet(today, calculateBalance(today));
	}
	
	//Generate month balance
	public String generateMonthBalance(){
		//Set the range of day to be calculated
		Calendar start = Calendar.getInstance();
		start.set( Calendar.DATE, 1 );
		Calendar end = Calendar.getInstance();
		float sum = 0;
		int count = 0;
		String str = "";
		String month = getMonthName(end.get(Calendar.MONTH));
		
		while (!start.after(end)){
		    Date thisDate = start.getTime();
		    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
			String day = df.format(thisDate);
			
			float balance = calculateBalance(day);
			sum += balance;
			
			if (balance != 0){
				str += balanceSheet(day, balance) + "\n";
				count++;
			}
			
		    start.add(Calendar.DATE, 1);
		}
		str += "\n===================================\n";
		str += "Total Balance at " + month + " = $" + sum + "\n";
		
		if (count > 1){
			str += "Average in " + month + " = $" + String.format("%.2f",sum/count) + "\n";
		}

		str += "===================================\n";
		return str;
	}
	
	public String getMonthName(int month){
		switch (month){
			case 0: return "January";
			case 1:	return "Februay";
			case 2: return "March";
			case 3: return "April";
			case 4: return "May";
			case 5: return "June";
			case 6: return "July";
			case 7: return "August";
			case 8: return "September";
			case 9: return "October";
			case 10: return "November";
			case 11: return "December";
			default: return "Invalid";
		}
	}
	
	private String balanceSheet(String day, float balance) {
		return ("Balance at " + day + " = $" + balance);
	}
	
	//Calculate the balance
	private float calculateBalance(String day){
		float balance = 0;
		
		for (int i = 0; i < logData.size(); i++){
			LogController.LogItem logItem = logData.elementAt(i);

			String name = logItem.getName();
			String type = logItem.getType();
			int qty = logItem.getQty();
			String date = logItem.getDate();
			
			if (date.equals(day)){
				if (type.equals("STOCK")){
					balance -= qty*getCost(name);
				}
				else if (type.equals("SALE")){
					balance += qty*getPrice(name);
				}
			}
		}
		return balance;
	}
	
	private float getPrice(String name){
		return pricelist.getPrice(name);
	}
	
	private float getCost(String name){
		return costlist.getCost(name);
	}
	
	public LogController getLogController(){
		//methods that should be accessed by JUnit test file only
		return logcontroller;
	}
}
